package com.club.football.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.club.football.common.UserHolder;
import com.club.football.dto.PageDto;
import com.club.football.dto.StaffSaveDto;
import com.club.football.entity.Staff;
import com.club.football.mapper.StaffMapper;
import com.club.football.req.StaffPageReq;
import com.club.football.service.StaffService;
import com.club.football.utils.BeanUtils;
import com.club.football.utils.R;
import org.springframework.stereotype.Service;

import static com.club.football.common.CommonConstants.*;

/**
 * 实施员工服务
 *
 * @author 1
 * @date 2024/01/09
 */
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements StaffService {
    @Override
    public R<Page<Staff>> staffPage(PageDto<Staff> pageDto, StaffPageReq staffPageReq) {
        String name = staffPageReq.getName();
        String phone = staffPageReq.getPhone();
        lambdaQuery().like(StringUtils.isNotBlank(name), Staff::getName, name)
                .eq(StringUtils.isNotBlank(phone), Staff::getPhone, phone)
                .page(pageDto);
        return R.ok(pageDto, GET_INFO_OK);
    }

    @Override
    public R<String> staffSave(StaffSaveDto saveDto) {
        Staff staff = BeanUtils.copier(saveDto, new Staff());
        Long userId = UserHolder.getUser().getUserId();
        staff.setUpdateBy(userId);
        if (staff.getStaffId() != null) {
            updateById(staff);
            return R.ok(EDIT_OK_MSG);
        }
        staff.setCreateBy(userId);
        save(staff);
        return R.ok(SAVE_OK_MSG);
    }
}
